package com.yanqu.road.dao.impl.activity.busyfarming;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.busyfarming.BusyFarmingHelpRecord;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class BusyFarmingHelpRecordDaoImpl extends TempDataObjectDao<BusyFarmingHelpRecord> {

    public long getMaxUserHelpDbId(int activityId) {
        String sql = "select ifnull(max(dbId),0) as maxId from t_u_activity_busy_farming_help_record where activityId=?";
        ResultSet rs = null;
        CallableStatement call = null;
        long maxId = 0L;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }

    public static String getBusyFarmingHelpRecordSql = "select * from t_u_activity_busy_farming_help_record where activityId=? and del=0";
    public Map<Long, List<BusyFarmingHelpRecord>> getBusyFarmingHelpRecord(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, List<BusyFarmingHelpRecord>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getBusyFarmingHelpRecordSql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                BusyFarmingHelpRecord data = parse(rs);
                if(data.isDel()){
                    continue;
                }
                if(!result.containsKey(data.getUserId())){
                    result.put(data.getUserId(), new ArrayList<>());
                }
                result.get(data.getUserId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertBusyFarmingHelpRecordSql = "insert into t_u_activity_busy_farming_help_record(dbId,userId,activityId,friendId,helpType,params,helpTime,del,readed,userBaseInfo) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(BusyFarmingHelpRecord data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getDbId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getFriendId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHelpType()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getParams(), "@#$")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHelpTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isDel()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReaded()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getBaseInfoJsonStr()));
        if (getDb().executeNoneQuery(insertBusyFarmingHelpRecordSql, para) > 0) {
            return true;
        }
        return false;
    }


    public static String updateBusyFarmingHelpRecordSql = "update t_u_activity_busy_farming_help_record set friendId=?,helpType=?,params=?,helpTime=?,del=?,readed=? where activityId=? and dbId=? and userId=?";

    @Override
    protected boolean updateData(BusyFarmingHelpRecord data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getFriendId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHelpType()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getParams(), "@#$")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHelpTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isDel()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReaded()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDbId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateBusyFarmingHelpRecordSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected BusyFarmingHelpRecord parseData(ResultSet rs) throws SQLException {
        BusyFarmingHelpRecord busyFarmingHelpRecord = new BusyFarmingHelpRecord();
        busyFarmingHelpRecord.setDbId(rs.getLong("dbId"));
        busyFarmingHelpRecord.setUserId(rs.getLong("userId"));
        busyFarmingHelpRecord.setActivityId(rs.getInt("activityId"));
        busyFarmingHelpRecord.setFriendId(rs.getLong("friendId"));
        busyFarmingHelpRecord.setHelpType(rs.getInt("helpType"));
        busyFarmingHelpRecord.setParams(StringUtils.stringToStringList(rs.getString("params"), "@#\\$"));
        busyFarmingHelpRecord.setHelpTime(rs.getInt("helpTime"));
        busyFarmingHelpRecord.setDel(rs.getBoolean("del"));
        busyFarmingHelpRecord.setReaded(rs.getBoolean("readed"));
        String str = rs.getString("userBaseInfo");
        if(!StringUtils.isNullOrEmpty(str)){
            UserBaseInfo userBaseInfo = JSON.parseObject(str, new TypeReference<UserBaseInfo>() {});
            if(null != userBaseInfo) {
                busyFarmingHelpRecord.setUserBaseInfo(userBaseInfo);
            }
        }
        return busyFarmingHelpRecord;
    }
}
